import { HOST } from '../../common/constant'
import { getDate, getDay } from '../../common/common'
import { dowImg } from '../../common/method'
import { getBackgroundToday,  apiAutoUpdateBgDownNumber } from '../../common/api'
let isShare = false
Page({
  data: {
    imgCompleted: false,
    statusBarHeight:0,
    todayTime: '-',
    todayDay: getDay(),
    showDialog: false,
    checkUrl: '',
    host: HOST,
    todayImg: ''
  },
  imgClick() {
    this.getTabBar((tabbar) => {
      tabbar.setData({
        tabBarActive: false
      })
    })

    this.setData({
      showDialog: true
    })
  },
  dialogClose() {
    this.getTabBar((tabbar) => {
      tabbar.setData({
        tabBarActive: true
      })
    })
    this.setData({
      showDialog: false
    })
  },

  async dowImg() {
    if(isShare) {
      const { todayImg } = this.data
      dowImg({ imgUrl: todayImg })
    } else {
      wx.showToast({
        title: '请先分享才可以下载哦',
        icon: 'none',
        mask:true
      })
    }
  },  

  getDateText() {
    let text = getDate().split('-')
    this.setData({
      todayTime: text[1] + '月' + text[2] + '日'
    })
  },

  getWindowInfos(){
    const { statusBarHeight } = wx.getWindowInfo()
    this.setData({
      statusBarHeight
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    getBackgroundToday().then(res => {
      this.setData({
        todayImg: res.url
      })
       
      wx.getImageInfo({
        src: HOST + res.url,
        success: () => {
          this.setData({
            imgCompleted:true
          })
        },
        fail:(err)=> {
          console.log(err)
        }
      })
    })
    this.getDateText()
    this.getWindowInfos()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    isShare = true
    apiAutoUpdateBgDownNumber({
      userId: wx.getStorageSync('userSignature')
    })
    return {
      title: '来选一张精美的壁纸吧'
    }
  }
})